package marmalius.creature;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class CreatureService {
	private static final CreatureService instance = new CreatureService();
	private Map<Integer, CreatureTemplate> creatureTemplates;
	
	public CreatureService() {
		creatureTemplates = new HashMap<Integer, CreatureTemplate>();
	}
	
	public boolean loadCreatureTemplate() {
		creatureTemplates.clear();
		try {
			int nb = 0;
			
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM creature_template");
			while (rs.next()) {
				creatureTemplates.put(rs.getInt("id"), new CreatureTemplate(rs.getString("name"), rs.getInt("combat"), rs.getInt("health")));
				nb++;
			}
			LogService.getInstance().logInfo("creature_template load ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load creature_template");
			LogService.getInstance().logError(ex.getMessage());
			return false;
		}
	}
	
	public static CreatureService getInstance() { return instance; }
}
